<template>
  <div class="app-container">
    <div class="table-box">
      <!-- 搜索区 开始 -->
      <div class="search-box">
        <el-form ref="listQuery" :model="listQuery" label-width="90px">
          <el-row>
            <el-col :xs="24" :sm="20" :md="20" :lg="20" :xl="20">
              <el-row>
                <!-- 表单元素 开始 -->
                <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
                  <el-form-item :label="$t('common.keywords')" prop="keywordsValue">
                    <el-input
                      v-model="listQuery.keywordsValue"
                      :placeholder="$t('common.pleaseEnter')+$t('common.keywords')"
                      class="input-with-select"
                      @keyup.enter.native="handleFilter"
                    >
                      <el-select
                        slot="prepend"
                        v-model="listQuery.keywordsKey"
                        :placeholder="$t('common.pleaseSelect')"
                        style="width: 90px"
                      >
                        <el-option
                          v-for="item in keywordKeysOptions"
                          :key="item.id"
                          :label="item.text"
                          :value="item.id"
                        />
                      </el-select>
                    </el-input>
                  </el-form-item>
                </el-col>
                <!-- 表单元素 开始 -->
              </el-row>
            </el-col>
            <el-col :xs="24" :sm="4" :md="4" :lg="4" :xl="4" class="search-btn">
              <el-button
                v-waves
                type="primary"
                icon="el-icon-search"
                @click="handleFilter"
              >
                {{ $t('common.search') }}
              </el-button>
              <el-button @click="handleResetFilter">{{ $t('common.reset') }}</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!-- 搜索区 结束 -->

      <el-table
        key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%"
        max-height="540"
        @sort-change="sortChange"
      >
        <el-table-column
          label="ID"
          prop="id"
          sortable="custom"
          align="center"
          width="80"
          :class-name="getSortClass('id')"
        >
          <template slot-scope="{ row }">
            <span>{{ row.id }}</span>
          </template>
        </el-table-column>

        <el-table-column
          :label="$t('common.user')"
          min-width="150px"
          align="center"
        >
          <template slot-scope="{ row }">
            {{ row.user_name }} 管理员姓名
          </template>
        </el-table-column>

        <el-table-column :label="$t('adminLog.operation_content')" min-width="150px">
          <template slot-scope="{ row }"> {{ row.name }} 操作内容 </template>
        </el-table-column>

        <el-table-column
          :label="$t('common.module')"
          min-width="150px"
          align="center"
        >
          <template slot-scope="{ row }"> {{ row.module }} 所属模块 </template>
        </el-table-column>

        <el-table-column
          :label="$t('common.methods')"
          min-width="150px"
          align="center"
        >
          <template slot-scope="{ row }"> {{ row.methods }} 请求方法 </template>
        </el-table-column>

        <el-table-column
          :label="$t('adminLog.request_ip')"
          min-width="150px"
          align="center"
        >
          <template slot-scope="{ row }"> {{ row.ip }} 请求ip </template>
        </el-table-column>

        <el-table-column
          :label="$t('common.created_at')"
          width="150px"
          align="center"
        >
          <template slot-scope="{ row }">
            <span>{{ row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>

        <el-table-column type="expand" :label="$t('common.operation')" width="150" fixed="right">
          <template slot-scope="props">
            <el-form label-position="left" inline class="table-expand">
              <el-form-item :label="$t('common.user')">
                <span>{{ props.row.user_name }}管理员姓名</span>
              </el-form-item>
              <el-form-item :label="$t('adminLog.operation_content')">
                <span>{{ props.row.name }} 操作内容</span>
              </el-form-item>
              <el-form-item :label="$t('common.module')">
                <span>{{ props.row.module }} 所属模块</span>
              </el-form-item>
              <el-form-item :label="$t('adminLog.request_methods')">
                <span>{{ props.row.methods }} 请求方法</span>
              </el-form-item>
              <el-form-item :label="$t('adminLog.request_uri')">
                <span>{{ props.row.request_uri }} 访问页面</span>
              </el-form-item>
              <el-form-item :label="$t('adminLog.request_ip')">
                <span>{{ props.row.ip }} 请求ip</span>
              </el-form-item>
              <el-form-item :label="$t('adminLog.request_parameters')">
                <span>{{ props.row.request_parameters }} 请求参数</span>
              </el-form-item>
              <el-form-item :label="$t('common.created_at')">
                <span>{{
                  props.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}')
                }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能 开始 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
      <!-- 分页功能 结束 -->
    </div>
  </div>
</template>

<script>
import { fetchList } from '@/api/article'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'AdminLogIndex',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        sort: '+id',
        keywordKey: '',
        keywordValue: ''
      },
      // 来源数据字典接口
      statusOptions: [
        { id: 1, text: '已发布' },
        { id: 2, text: '未发布' }
      ],
      keywordKeysOptions: [
        { id: 'user_name', text: '管理员姓名' },
        { id: 'name', text: '操作行为' },
        { id: 'module', text: '所属模块' },
        { id: 'ip', text: 'ip' }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then((response) => {
        this.list = response.data.items
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 重置搜索表单
    handleResetFilter() {
      this.listQuery.page = 1
      this.$refs['listQuery'].resetFields()
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>
<style scoped  lang="scss" >
</style>
